package com.turman.networkdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.google.gson.Gson
import com.turman.networkdemo.databinding.ActivityMainBinding
import okhttp3.Response

class MainActivity : AppCompatActivity() {
    private lateinit var binding:ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        var map:Map<String,String> = mapOf(
            "key" to "free",
            "appid" to "0",
            "msg" to "你好"
        )
        val httpApi:HttpApi = OkHttpApi()

        httpApi.get(map,"api.php",object:IHttpCallback{
            override fun onSuccess(dat: Any?) {
                Log.i("MainActivity","success result:${dat.toString()}")
                runOnUiThread{
                    binding.result.text = (dat as Response).body?.string() ?: ""
                }
            }

            override fun onFailed(error: Any?) {
                Log.i("MainActivity","failed result:${error}")
            }
        })

        Thread{
            val response:Response = httpApi.getSync(map,"api.php") as Response
            runOnUiThread{
                Toast.makeText(this@MainActivity, response.body?.string(), Toast.LENGTH_LONG).show()
            }

        }.start()
    }
}